import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'
axios.defaults.baseURL = "http://localhost:99/api"
    //解决路由重复点击产生报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
const routes = [{
        path: '/',
        redirect: '/welcome'
    },
    {
        path: '/login',
        component: () =>
            import ( /* webpackChunkName: "login" */ '../views/Login.vue'),
    },
    {
        path: '/welcome',
        name: 'welcome',
        component: () =>
            import ( /* webpackChunkName: "welcome" */ '../views/front/shouye.vue')
    },
    {
        path: '/front',
        name: 'Front',
        redirect: '/home',
        component: () =>
            import ( /* webpackChunkName: "front" */ '../views/front/Front.vue'),
        children: [{
                path: '/home',
                name: 'Home',
                component: () =>
                    import ( /* webpackChunkName: "front" */ '../views/front/Home.vue'),
                redirect: '/home/blog',
                children: [{
                    path: 'blog',
                    name: 'blog',
                    component: () =>
                        import ( /* webpackChunkName: "front" */ '../components/front/home/blog.vue')
                }, {
                    path: '/home/blogDetail',
                    name: 'blogDetail',
                    component: () =>
                        import ( /* webpackChunkName: "front" */ '../components/front/home/blogDetail.vue')
                }, {
                    path: 'search',
                    name: 'search',
                    component: () =>
                        import ( /* webpackChunkName: "front" */ '../components/front/home/search.vue')
                }]
            },
            {
                path: '/time',
                name: 'Time',
                component: () =>
                    import ( /* webpackChunkName: "front" */ '../views/front/Time.vue')
            },
            {
                path: '/about',
                name: 'About',
                component: () =>
                    import ( /* webpackChunkName: "front" */ '../views/front/About.vue')
            }
        ]
    },
    {
        path: '/back',
        name: 'Back',
        component: () =>
            import ( /* webpackChunkName: "back" */ '../views/back/Back.vue'),
        redirect: '/admin',
        children: [{
                path: '/admin',
                name: 'Admin',
                component: () =>
                    import ( /* webpackChunkName: "back" */ '../views/back/Admin.vue'),
                children: [{
                    path: 'types',
                    name: 'types',
                    component: () =>
                        import ( /* webpackChunkName: "back" */ '../components/back/types.vue')
                }, {
                    path: 'tags',
                    name: 'tags',
                    component: () =>
                        import ( /* webpackChunkName: "back" */ '../components/back/tags.vue')
                }, {
                    path: 'artical',
                    name: 'artical',
                    component: () =>
                        import ( /* webpackChunkName: "back" */ '../components/back/artical.vue')
                }]
            },
            {
                path: '/write',
                name: 'Write',
                component: () =>
                    import ( /* webpackChunkName: "back" */ '../views/back/Write.vue')
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => { //to是去的路径，from是当前的路径
    //如果访问的路径为后台路径,则获取token,判断是否已经登录
    if (to.path.indexOf('/admin') == 0 || to.path == '/write') {
        let session = window.sessionStorage.getItem('token')
            //todo
            //携带token向后端发送请求,后端返回200才能继续访问
        if (!session) {
            return next('/login') //跳转到/login页面
        }
        axios({
            method: 'post',
            url: '/blog/admin/loginToken',
            headers: { 'Authorization': 'Bearer ' + window.sessionStorage.getItem('token') }
        }).then(res => {
            if (res.data && res.data.meta.status != 200) {
                return next('/login') //跳转到/login页面
            }
        }).catch(e => {
            return next('/login') //跳转到/login页面
        })
    }
    next(); //最后必须写上next()语句保证继续执行,不写就相当于被拦截了
})

export default router